# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from bd_spider.config import SpiderConfig
from bd_spider.dal import baijiahao_dal
from bd_spider.dal import article_dal
from bd_spider.dal import hot_dal
from bd_spider.action import simulation

class FakeBrowser(object):
    def open_spider(self, spider):
        if spider.name in (SpiderConfig.BAIJIAHAO_SPIDER_NAME, SpiderConfig.ARTICLE_SPIDER_NAME):
            simulation.init_browser()

    def close_spider(self, spider):
        if spider.name in (SpiderConfig.BAIJIAHAO_SPIDER_NAME, SpiderConfig.ARTICLE_SPIDER_NAME):
            simulation.quit_browser()

""" Redis 过滤模块已经移除 (2018/08/24) """
class Filter(object):
    def process_item(self, item, spider):
        if spider.name == SpiderConfig.BAIJIAHAO_SPIDER_NAME:
            baijiahao_dal.filter(item)
        if spider.name == SpiderConfig.ARTICLE_SPIDER_NAME:
            article_dal.filter(item)
        return item


class Injector(object):
    def open_spider(self, spider):
        if spider.name == SpiderConfig.HOT_SPIDER_NAME:
            hot_dal.set_invalid()

    def process_item(self, item, spider):
        if spider.name == SpiderConfig.BAIJIAHAO_SPIDER_NAME:
            baijiahao_dal.inject(item)
        if spider.name == SpiderConfig.ARTICLE_SPIDER_NAME:
            article_dal.inject(item)
        if spider.name == SpiderConfig.HOT_SPIDER_NAME:
            hot_dal.inject(item)
        return item

